//
//  _BaseCertObject.m
//  TestProject
//
//  Created by 武侠 on 2022/2/18.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "_BaseCertObject.h"
#import "CertItemLength.h"

@implementation _BaseCertObject
// 30 10 82 0E 77 77 77 2E 67 6F 6F 67 6C 65 2E 63 6F 6D
- (void)loadCertData {
    char *data = (char *)self.data.bytes;
    int end = (int)self.data.length;
    
    // 计算数组的长度信息
    CertItemLength *length = [[CertItemLength alloc] initWithData:data index:1];
    int i = 1 + length.lenLen;      // 偏移数组长度
    while (i < end) {
        uint8_t type = data[i];     // 获取类型
        NSString *key = self.objectType[@(type)];
        if (key != nil) {
            length = [[CertItemLength alloc] initWithData:data index:i+1];
            NSData *vData = [NSData dataWithBytes:data+i length:1+length.lenLen+length.len];
            id value = [self getObject:vData type:type];
            if (value != nil) {
                [self setValue:value forKey:key];
            }
        }
        i += 1 + length.lenLen + length.len;
    }
}

- (id _Nullable)getObject:(NSData *)data type:(uint16_t)type {
    return nil;
}

// 加载：数组类型的数组
- (NSArray *)getList:(NSData *)sdata type:(uint16_t)type {
    char *data = (char *)sdata.bytes;
    uint8_t ltype = data[0];
    if (ltype != 0x30 && ltype != 0x31) {
        return @[];
    }
    
    int end = (int)sdata.length;
    CertItemLength *length = [[CertItemLength alloc] initWithData:data index:1];
    int i = 1 + length.lenLen;
    
    NSMutableArray *array = [NSMutableArray array];
    while (i < end) {
        length = [[CertItemLength alloc] initWithData:data index:i+1];
        NSData *vData = [NSData dataWithBytes:data+i length:1+length.lenLen+length.len];
        id obj = [self getObject:vData type:type + 0x100];
        if (obj != nil) {
            [array addObject:obj];
        }
        i += 1 + length.lenLen + length.len;
    }
    return array;
}

@end
